//-----------------------------------------------------------------------
// <copyright file="DeleteCertificateMapping.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>DeleteCertificateMapping</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Delete a Certificate Mapping
    /// </summary>
    public class DeleteCertificateMapping : TaskBase
    {
        /// <summary>
        /// The description of the web site to locate
        /// </summary>
        private string description;

        /// <summary>
        /// The identifier of the website to locate
        /// </summary>
        private string identifier;

        /// <summary>
        /// The username that is to be un-mapped
        /// </summary>
        private string accountUsername;

        private string machineName = "localhost";

        /// <summary>
        /// Gets or sets the name of the machine to operate on. Default is localhost
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// The username that is to be un-mapped
        /// </summary>
        [Required]
        public string AccountUsername
        {
            get { return this.accountUsername; }

            set { this.accountUsername = value; }
        }

        /// <summary>
        /// Description of web site to modify
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Identifier">Identifier</seealso> 
        /// for the site you wish to start</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }

            set { this.description = value; }
        }

        /// <summary>
        /// web site idenfitier to modify
        /// </summary>
        /// <value>A valid web site identifier</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// for the site you wish to start</remarks>
        public string Identifier
        {
            get { return (this.identifier == null ? String.Empty : this.identifier); }

            set { this.identifier = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            if (this.description == null && this.identifier == null)
            {
                throw new TaskException("Web.DescriptionOrIdentifier");
            }

            if (this.description != null && this.identifier != null)
            {
                throw new TaskException("Web.DescriptionOrIdentifierNotBoth", this.description, this.identifier);
            }

            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.description, this.identifier, "Delete Certificate Mapping", this.MachineName);
            if (Microsoft.Sdc.Tasks.Configuration.Web.WebSite.Exists(this.MachineName, id))
            {
                Microsoft.Sdc.Tasks.Configuration.Web.WebSite x = Microsoft.Sdc.Tasks.Configuration.Web.WebSite.Load(this.MachineName, id);
                x.DeleteCertificateMapping(this.AccountUsername);
            }
        }
    }
}